<?php

namespace app\admin\controller\Subject;

use app\common\controller\Backend;

use think\exception\PDOException;
use Exception;

use app\common\model\Subject\Category as C;

class Category extends Backend
{
    // 当前对象模型
    protected $model = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = model('Subject.Category');
    }

    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            // ->with('category')
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        // halt($list);
        // exit;

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    // 新增课程
    public function add()
    {
        // $CateData = model('Subject.Category')->order('weight DESC')->column('id,name');

        if ($this->request->isPost()) {
            $params = $this->request->param('row/a');

            if (empty($params)) {
                $this->error('参数项不能为空');
            }

            // $result = $this->model->validate('common/Subject/Subject')->save($params);
            $result = $this->model->save($params);

            if ($result === FALSE) {
                if (!empty($params['thumbs'])) {
                    $thumbsArr = explode(',', $params['thumbs']);

                    $thumbsArr = array_filter($thumbsArr);

                    foreach ($thumbsArr as $thumb) {
                        @is_file('.' . $thumb) && @unlink('.' . $thumb);
                    }
                }
                $this->error($this->model->getError());
            } else {
                $this->success();
            }

            $this->assign([
                'CateData' => $params
            ]);
        }

        return $this->fetch();
    }

    public function edit($ids = 0)
    {
        if ($this->request->isPost()) {
            $params = $this->request->param('row/a');

            if (empty($params)) {
                $this->error('参数项不能为空');
            }

            // $result = $this->model->validate('common/Subject/Subject')->save($params);
            $result = $this->model->isUpdate(true)->save(['id' => $ids, 'name' => $params['name']]);

            if ($result) {
                $this->success();
            }
        }

        // $CateData = $this->model->order('weight DESC')->column('id,name');
        $CateData = $this->model->where('id',$ids)->find();

        $this->assign([
            'CateData' => $CateData
        ]);
        return $this->fetch();
    }

    // 软删除 => 不是在数据库把数据真实删除
    public function del($ids = null)
    {
        $row = $this->model->whereIn('id', $ids)->select();

        if (!$row) {
            $this->error('请选择需要删除的数据');
        }

        $count = 0;

        $this->model->startTrans();

        try {
            foreach ($row as $item) {
                $count += $item->delete();
            }
            $this->model->commit();
        } catch (PDOException | Exception $e) {
            $this->model->rollback();
            $this->error($e->getMessage());
        }

        if ($count > 0) {
            $this->success();
        } else {
            $this->error();
        }
    }
}
